package com.cn.shine.controller;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cn.shine.common.BaseBean;
import com.cn.shine.common.utils.DateUtil;
import com.cn.shine.pojo.Customer;
import com.cn.shine.pojo.Order;
import com.cn.shine.pojo.PayType;
import com.cn.shine.service.CustomerService;
import com.cn.shine.service.OrderService;
import com.cn.shine.service.PayTypeService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

@Controller
@RequestMapping("/customer")
public class CustomerController {

	@Resource
	private CustomerService customerService;
	
    @Resource  
    private PayTypeService payTypeService;
	
    @Resource
    private OrderService orderService;
    
	@RequestMapping("/customerList")
	public String customerList(){
		return "customerList";
	}
	
	@RequestMapping("/iHaveNot")
	public String iHaveNot(){
		System.out.println(2312);
	    return "iHaveNot";
	}
	@RequestMapping("/stopCustomerList")
	public String stopCustomerList(){
		return "stopCustomerList";
	}
    @RequestMapping("/addCustomer")
    @ResponseBody
	public JSONObject addCustomer(Customer customer) {
		BaseBean bean = new BaseBean();
		Customer c = new Customer();
		c.setcNum(customer.getcNum());
		int count =customerService.getCountCustomerList(c);
		if(count>0){
			  bean.setResult("0");
              bean.setMsg("会员编号已存在哦！");
              return JSONObject.fromObject(bean);
		}
		int i = customerService.addCustomer(customer);
		if (i == 0) {
			bean.setResult("0");
			bean.setMsg("添加失败");
		} else {
			bean.setMsg("添加成功");
			bean.setResult("1");
		}

		return JSONObject.fromObject(bean);
	}
	
    @RequestMapping("/getCustomerListByPage")
    @ResponseBody
	public JSONObject getCustomerListByPage(Customer customer){
		List<Customer> list = customerService.getCustomerListByPage(customer);
		int count = customerService.getCountCustomerList(customer);
		JSONObject jsonObject=new JSONObject();
        Gson gson = new GsonBuilder().create();
        jsonObject.put("total", count);
        jsonObject.put("rows", gson.toJson(list));
        return jsonObject;
	}
    
    @RequestMapping("/updateCustomer")
    @ResponseBody
    public JSONObject updateCustomer(Customer customer){
    	JSONObject jsonObject=new JSONObject();
    	BaseBean bean = new BaseBean();
    	int i =customerService.updateByPrimaryKeySelective(customer);
		if (i == 0) {
			bean.setResult("0");
			bean.setMsg("更新失败");
		} else {
			bean.setMsg("更新成功");
			bean.setResult("1");
		}
		return JSONObject.fromObject(bean);
    }
    
    @RequestMapping("/deleteCustomer")
    @ResponseBody
    public JSONObject deleteCustomer(int id){
    	JSONObject jsonObject=new JSONObject();
    	BaseBean bean = new BaseBean();
    	int i = customerService.deleteByPrimaryKey(id);
    	if (i == 0) {
			bean.setResult("0");
			bean.setMsg("删除失败");
		} else {
			bean.setMsg("删除成功");
			bean.setResult("1");
		}
		return JSONObject.fromObject(bean);
    }
    
    @RequestMapping("/findCustomerById")
    @ResponseBody
    public JSONObject findCustomerById(int id,String payType){
    	JSONObject json = new JSONObject();
    	Customer customer = customerService.selectByPrimaryKey(id);
    	json = JSONObject.fromObject(customer);
        if(StringUtils.isNotEmpty(payType)){
        	Gson gson = new GsonBuilder().create();
        	List<PayType> list = payTypeService.getPayTypeList();
        	json.put("payTypeList", gson.toJson(list));
        	Order o = new Order();
        	o.setCustomerId(id);
        	o.setPageSize(1);
        	o.setPageStart(0);
        	List<Order> orderlist = orderService.getOrderListByPage(o);
        	if(orderlist!=null&&orderlist.size()>0){
        		Order order = orderlist.get(0);
        		if(StringUtils.isNotEmpty(order.getPayDate()));
        		try {
					Date date = DateUtil.StringToDate(order.getPayDate(), "yyyy-MM-dd HH:mm:ss");
					long between  = new Date().getTime()-date.getTime();
					if (between/1000/60/60<2){
						json.put("care", "注意哟，该用户两小时内有已有一笔金额为"+order.getTotalMoney()+"的消费。");
					}
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        	}
        }
    	return json;
    }
    
    @RequestMapping("/exportCustomer")
    public void exportCustomer(HttpServletRequest request,
            HttpServletResponse response,Customer customer){
       customerService.exportCustomer(request, response, customer);
        
    }
    
}
